<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport"
			content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../../css/header.css" />
	</head>

	<body>

		<header class="mui-bar mui-bar-nav title">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" style="color: white;"></a>
			<h1 class="mui-title title-color">实习预约申请</h1>
		</header>

		<div class="mui-content">
			<ul class="mui-table-view" id="ul_enterprise_list" style="margin-bottom: 10px;">
				<!-- <li class="btnOper mui-table-view-cell mui-media">
					<a href="javascript:;">
						<img class="mui-media-object mui-pull-left" src="../../images/1.jpg">
						<span id="span_nickname" class="mui-pull-right">
							<p style="font-weight: bold; color: green; line-height: 40px; margin-right: 5px;">通过</p>
							<button friendId="" type="button" class="ignoreRequest mui-btn mui-btn-green" style="cenpadding: 4px 10px;margin-right: 5px;">申请</button>
						</span>
						<div class="mui-media-body">
							<label>aaa</label>
							<p class="mui-ellipsis">地址</p>
						</div>
					</a>
				</li> -->
			</ul>

		</div>

		<script src="../../js/mui.min.js"></script>
		<script src="../../js/app.js"></script>
		<script type="text/javascript">
			mui.init()
			var userinfo;
			mui.plusReady(function() {
				userinfo = app.getGlobalUserInfo();
				// 加载企业列表
				loadEnterpriseList();
			});
			
			// 加载企业列表，并且每个企业对于当前学生的实习申请状态
			function loadEnterpriseList() {
				mui.ajax(app.serverUrl + "/user/student/getEnterprisesAndIa_status", {
					data:{
						user_id: userinfo.user_id
					},
					dataType:'json',//服务器返回json格式数据
					type:'post',//HTTP请求类型
					timeout:10000,//超时时间设置为10秒；
					headers:{'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'},	              
					success:function(data){
						if(data.status == 200) {
							// 获取页面ul元素
							var ul_enterprise_list = document.getElementById("ul_enterprise_list");
							var enterprises = data.data;
							var enterprise_lis = [];
							for(var i = 0; i < enterprises.length; i++) {
								enterprise_lis.push(renderEnterpriseLi(enterprises[i]));
							}
							ul_enterprise_list.innerHTML = enterprise_lis.join("");
							
							// 给每一个企业信息列表项添加点击事件
							mui(".enterprise_li").on("tap", ".submitApply", function() {
								// 获取企业编号
								var e_id = this.getAttribute("e_id");
								// 对点中的企业提交实习申请
								submitInternshipApplication(e_id);
							});
						} else {
							app.showToast(data.msg, "error");
						}
					},
					error:function(xhr,type,errorThrown){
						//异常处理；
						console.log(type);
					}
				});
			}
			
			// 对点中的企业提交实习申请
			function submitInternshipApplication(e_id) {
				mui.ajax(app.serverUrl + "/user/student/submitInternshipApplication", {
					data:{
						user_id: userinfo.user_id,
						ia_e_id: e_id
					},
					dataType:'json',//服务器返回json格式数据
					type:'post',//HTTP请求类型
					timeout:10000,//超时时间设置为10秒；
					headers:{'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'},	              
					success:function(data){
						if(data.status == 200) {
							app.showToast("已提交申请！", "success");
							loadEnterpriseList();
						} else {
							app.showToast(data.msg, "error");
						}
					},
					error:function(xhr,type,errorThrown){
						//异常处理；
						console.log(type);
					}
				});
			}
			
			// 渲染实习公司列表中的某一项
			function renderEnterpriseLi(enterprise) {
				// 如果ia_status不为空，说明当前学生对该企业已经提交过实习申请，
				// 应该显示审核状态，不应该显示申请按钮
				var operator = "";
				var ia_status = enterprise.ia_status;
				if(ia_status != null) {
					var ia_status_str;
					var ia_status_color;
					if(ia_status == 0) {
						ia_status_str = "待审核";
						ia_status_color = "blue";
					} else if(ia_status == 1) {
						ia_status_str = "通过";
						ia_status_color = "green";
					} else if(ia_status == 2) {
						ia_status_str = "未通过";
						ia_status_color = "red";
					}
					operator = '<p style="font-weight: bold; color: '+ ia_status_color +'; line-height: 40px; margin-right: 5px;">' + ia_status_str + '</p>';
				} else {
					operator = '<button e_id="' + enterprise.e_id + '" type="button" class="submitApply mui-btn mui-btn-green" style="padding: 4px 10px;margin-right: 5px;">申请</button>';
				}
				
				return  '<li class="enterprise_li mui-table-view-cell mui-media">' +
						   '<a href="javascript:;">' +
							'<img class="mui-media-object mui-pull-left" src="../../images/1.jpg">' +
							'<span id="span_nickname" class="mui-pull-right">' +
								operator +
							'</span>' +
							'<div class="mui-media-body">' +
								'<label>' + enterprise.e_name + '</label>' +
								'<p class="mui-ellipsis">地址：' + enterprise.e_address + '</p>' +
							'</div>' +
						   '</a>' +
						'</li>';
			}
		</script>
	</body>

</html>
